﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Repository.MiniSite;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
using YunSpace.Domain.ViewModel.MiniSite;
#endregion

namespace YunSpace.Application.MiniSite
{
    public class CommentApp
    {
        private ICommentRepository service = new CommentRepository();

        public List<SiteCommentEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<SiteCommentEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public SiteCommentEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(SiteCommentEntity commentEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                commentEntity.Modify(keyValue);
                service.Update(commentEntity);
            }
            else
            {
                commentEntity.Create();
                // commentEntity.F_Category = 2;
                service.Insert(commentEntity);
            }
        }


        /// <summary>
        /// 获取评价-list
        /// </summary>
        /// <param name="lastId">最后一个id</param>
        /// <param name="count">获取条数</param>
        /// <param name="input">可单独传参</param>
        /// <returns></returns>
        public List<CommentActionModel> GetComment(string lastId, int count, CommentActionModel input)
        {

            using (var db = new RepositoryBase())
            {
                var result1 = new List<CommentActionModel>();

                var strSql = @" select mc.F_CustomerId,mc.F_SiteId,mc.F_SpaceId,mc.F_OrderId,mc.F_Content,mc.F_Star,mc.F_CreatorTime,mc.F_LastModifyTime,m.F_Avatar as path ,m.F_NickName
                            
                                from MiniSite_Comment mc inner join MiniSite_Customer m on m.F_Id=mc.F_CustomerId {0}  ORDER BY mc.F_CreatorTime DESC LIMIT 0, {1};";

                var sql = "";
                if (!input.F_CustomerId.IsNullOrEmpty())
                {
                    sql += " and mc.F_CustomerId=@F_CustomerId";
                }
                if (!input.F_SiteId.IsEmpty())
                {
                    sql += " and mc.F_SiteId=@F_SiteId";
                }
                if (!input.F_SpaceId.IsNullOrEmpty())
                {
                    sql += " and mc.F_SpaceId=@F_SpaceId";
                }
                if (!input.F_OrderType.IsEmpty()&&!input.F_OrderId.IsNullOrEmpty())
                {
                    sql += " and mc.F_OrderType=@F_OrderType";
                    sql += " and mc.F_OrderId=@F_OrderId";
                }
                if (lastId.IsNullOrEmpty())
                {
                    strSql = string.Format(strSql, sql, count);
                }
                else
                {
                    strSql = string.Format(strSql, "AND mc.F_Id > @fId"+sql, count);
                }
                DbParameter[] parameter = {
                    new MySqlParameter("@fId", lastId),
                    new MySqlParameter("@F_CustomerId", input.F_CustomerId),
                    new MySqlParameter("@F_SiteId", input.F_SiteId),
                    new MySqlParameter("@F_SpaceId", input.F_SpaceId),
                    new MySqlParameter("@F_OrderId", input.F_OrderId),
                    new MySqlParameter("@F_OrderType", input.F_OrderType)
                };
                result1 = db.FindList<CommentActionModel>(strSql, parameter);
                return result1;
            }
        }

    }
}

